<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/23
  Time: 13:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <span>请选择商品分类：</span>
  <select id="sortlist">
    <option value="0">--请选择--</option>
  </select>
  <table id="li" >
    <tr class="firstTr">
      <td >商品编号</td>
      <td >商品名称</td>
      <td >商品分类</td>
      <td >产地</td>
      <td >生产日期</td>
      <td >单价</td>
      <td >剩余数量</td>
      <td >购买</td>
    </tr>
  </table>
  <div class="page-bar">
    <ul class="page-num-ul clearfix">
      <li>共<span id="totalCount"></span>条记录&nbsp;&nbsp; <span id="currentPage"></span>/<span id="totalPage"></span>页</li>

      <a href="javascript:void(0)" class="first">首页</a>
      <a href="javascript:void(0)" class="prev">上一页</a>


      <a href="javascript:void(0)" class="next">下一页</a>
      <a href="javascript:void(0)" class="last">最后一页</a>

      &nbsp;&nbsp;
    </ul>
    <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key" />页
	     <button type="button" class="page-btn" onClick=''>GO</button>
		</span>
  </div>
  </body>
<script src="js/jquery-1.9.1.min.js"></script>
<script>

  var currentPage =1;
      $(function () {
        bindsort()
        bindGood(currentPage)
        bindEvent()
})
       function bindGood(currentPage) {
       var name=$("#sortlist").val()
        $.post("list",{"name":name,"currentPage":parseInt(currentPage)},function (mag) {
        var str=""
        $(mag.list).each(function (i) {
        var g=mag.list[i]
          str+="<tr>"
          str+="<td>"+g.id+"</td>"
          str+="<td>"+g.name+"</td>"
          str+="<td>"+g.goodSort.name+"</td>"
          str+="<td>"+g.address+"</td>"
          str+="<td>"+formartDate(g.createDate)+"</td>"
          str+="<td>"+g.price+"</td>"
          str+="<td>"+g.remaining+"</td>"
          str+="<td>"
          str+='<a>购买</a>'
          str+="</td>"
          str+="</tr>"
       })
          $("table tr:not(:first)").remove();
          $("#li").append(str)
   },"json")

}
    function bindsort() {
      $.post("listsort",function(msg){
        var str = "";
     $(msg).each(function (i) {
        var s=msg[i]
       str+="<option value='"+s.id+"'>"+ s.name + "</option>"
        })
        $("#sortlist").append(str)
   },"json")
}
      function formartDate(obj){
        var d = new Date();
        d.setTime(obj);
        var str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
        return str;
  }



      function bindEvent(){
        //首页
        $(".first").click(function(){
          currentPage = 1;
          bindUser(currentPage);
        })
        //上一页
        $(".prev").click(function(){
          if(currentPage == 1){
            return;
          }
          currentPage--;
          bindUser(currentPage);
        })
        //下一页
        $(".next").click(function(){
          if(currentPage == $("#totalPage").html()){
            return;
          }
          currentPage++;
          bindUser(currentPage);
        })

        //最后一页
        $(".last").click(function(){
          currentPage = parseInt($("#totalPage").html());
          bindUser(currentPage);
        })

        //跳转页按钮
        $(".page-btn").click(function(){
          var inputPage = $("#inputPage").val();
          if(isNaN(inputPage)){
            alert("请正确输入页码数")
            return;
          }
          if(parseInt(inputPage) < 1 || parseInt(inputPage) > parseInt($("#totalPage").html())){
            alert("请正确输入页码数")
            return;
          }
          currentPage = parseInt(inputPage);
          bindUser(currentPage);
        })
      }
</script>
</html>
